﻿package com.kuanter.kuanterauto.baseui;

import java.util.Stack;


import android.app.AlertDialog;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;

import com.kuanter.kuanterauto.param.ActivityHolder;
import com.kuanter.kuanterauto.utils.DataLoader;


public class ProApplication extends Application {
	private static ProApplication instance;

	public static final int KUANTER_WD = 0;//网点
	public static final int KUANTER_CX = 1;//车险
	public static final int KUANTER_HY = 2;//会员
	public static final int KUANTER_XX = 3;//消息
	public static final int KUANTER_SZ = 4;//设置
	
	
	public static String downloadDir = "kuanterauto/";// 安装目录
	

	DataLoader loader=null;
	
	public static Stack<ActivityHolder> actStack = new Stack<ActivityHolder>(); // 存放act跳转

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {

		
			switch (msg.what) {
			case 1:
				Bundle bundle = msg.getData();
				String json = bundle.getString("result");
				 System.out.println("---------------------" + json);
//				try {
//					if (StringUtils.isSuccess(json)) {
//						JSONObject jobj = new JSONObject(json);
//						if(jobj.optInt("version") > 0)
//							serverVersion = jobj.optInt("version");
//					}
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				
				break;
			case -1:
				break;
			}
		};
	};
	
	
	
	public ProApplication() {
		instance = this;
//		loader=new DataLoader(this);
		
		 
		 
	}

	public static Context getContext() {
		return instance;
	}
	
	//判断网络是否可用
		public boolean CheckNetworkState()
	    {
	        boolean flag = false;
	        ConnectivityManager manager = (ConnectivityManager)getSystemService(
	                Context.CONNECTIVITY_SERVICE);
	        if(manager.getActiveNetworkInfo() != null)
	        {
	            flag = manager.getActiveNetworkInfo().isAvailable();
	        }
	        if(!flag)
	        {
	            AlertDialog.Builder builder = new AlertDialog.Builder(this);
	            builder.setIcon(android.R.drawable.ic_dialog_alert);
	            builder.setTitle("网络状况");
	            builder.setMessage("当前网络不可用，是否设置网络");
	            builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
	                
	                @Override
	                public void onClick(DialogInterface dialog, int which) {
	                    // TODO Auto-generated method stub
	                    Intent mIntent = new Intent("/");
	                    ComponentName comp = new ComponentName("com.android.settings",
	                            "com.android.settings.WirelessSettings");
	                    mIntent.setComponent(comp);
	                    mIntent.setAction("android.intent.action.VIEW");
	                    startActivity(mIntent);
	                }
	            });
	            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
	                
	                @Override
	                public void onClick(DialogInterface dialog, int which) {
	                    dialog.cancel();
	                }
	            });
	            builder.create();
	            builder.show();
	        }
	        
	        return flag;
	        
	    }
}
